<?php
include_once "function_xml_basic.php";
class base_xml_class {
	protected $root_path;
	protected $node_name;
	protected $key_array;

	function __construct($root, $node, $key) 
	{
		$this->root_path = $root;
		$this->node_name = $node;
		$this->key_array = $key;
	}
	private function get_root_path()
	{
		return $this->root_path;
	}
	private function get_node_name()
	{
		return $this->node_name;
	}
	private function get_key_array()
	{
		return $this->key_array;
	}
	private function get_xml_path()
	{
		$query_string = $this->get_root_path(). "/". $this->get_node_name();
		return $query_string;
	}
	
	public function get_list()
	{
		$query_string = $this->get_xml_path();
		return GetAttributList($query_string, $this->get_key_array());
	}
	public function get_list_number()
	{
		$query_string = $this->get_xml_path();
		$number = GetNumberOfNode($query_string);
		return $number;
	}
	public function get_item_by_num($num)
	{
		$query_string = $this->get_xml_path();
		$ret_list = GetAttributList($query_string, $this->get_key_array());
		return $ret_list[$num];
	}
	public function get_item_by_name($name)
	{
		$ret_list = $this->get_item_by_attr("Name", $name);
		//$query_string = $this->get_xml_path(). "[@Name='". $name. "']";
		//$ret_list = GetAttributList($query_string, $this->get_key_array());
		return $ret_list;
	}
	public function exist_item_by_name($name)
	{	
		$iflag = 0;
		$ret_list = $this->get_item_by_attr("Name", $name);
		foreach ($ret_list as $l){
			if ($l['Name'] == $name){
				$iflag = 1;
				break;
			}
		}
		return $iflag;
	}
	public function get_item_by_attr($attr,$v)
	{
		$query_string = $this->get_xml_path(). "[@". $attr. "='". $v. "']";
		$ret_list = GetAttributList($query_string, $this->get_key_array());
		return $ret_list;
	}
	public function append_item($Valu_array)
	{
		$query_string = $this->get_root_path();
		$node_name = $this->get_node_name();
		AppendAllAttrOfNode($query_string, $node_name, $Valu_array);	
		$Valu_array['Operate'] = 'Add';
		$this->syslog_r($Valu_array);
	}
	function insert_item_by_num($number, $in_array)
	{
	 	$query_string = $this->get_xml_path();
		$node_name = $this->get_node_name();
	 	$query_string_from = $query_string. '['. $number. ']';
	 	InsertOneNode($query_string_from, $node_name, $in_array);
	}
	public function edit_item_by_num($number, $Valu_array)
	{
		$query_string = $this->get_xml_path();
		$query_string .= "[". ($number + 1). "]";
		EditAllAttrOfNode($query_string, $Valu_array);
	}
	public function edit_item_by_name($name, $Valu_array)
	{	
		$query_string = $this->get_xml_path();
		$query_string .= "[@Name='". ($name). "']";
		EditAllAttrOfNode($query_string, $Valu_array);
	}
	public function del_special_item_by_num($number)
	{
		$query_string = $this->get_xml_path();
		$query_string .= "[". ($number + 1). "]";
		DelSpecialNode($query_string);	
	}
	public function del_special_item_by_name($name)
	{
		$attr = "Name";
		$this->del_special_item_by_attr($attr, $name);
	}
	public function del_special_item_by_attr($attr,$value)
	{
		$query_string = $this->get_xml_path();
		$query_string .= "[@".$attr."='". ($value). "']";
		DelSpecialNode($query_string);	
	}
	public function del_all_item()
	{
		$query_string = $this->get_xml_path();
		DelSpecialNode($query_string);
	}
	public function move_item($from, $to)
	{
		$query_string = $this->get_xml_path();
		$query_string_from = $query_string. "[". $from. "]";
		$query_string_to = $query_string. "[". $to. "]";
		$total = $this->get_list_number();
		
		if ($to > $total || $from > $total || $to == $from)
			return ;

		if ($from > $to)
		{
			MoveFrontOneNode($query_string_from, $query_string_to);
		}
		else
		{
			$number = $this->get_list_number();
			if ($to < $number)
			{
				$query_string_to = $query_string. "[". ($to + 1 ). "]";
				MoveFrontOneNode($query_string_from, $query_string_to);
			}
			else
			{
				MoveEndOneNode($query_string_from, $query_string_to);
			}
		}
	}
	private function syslog_r($a)
	{
		$s = " ";
		foreach($a as $r) {	
			$k = key($a);
			$s .= $k. '='. $r. ' ';
			next($a);
		}

		define_syslog_variables();
		openlog("FireWall", LOG_PERROR, LOG_LOCAL0);
        	$access = date("Y/m/d H:i:s");
    		syslog(LOG_WARNING, 
		       "Time='". $access. "' User=". $_SESSION['UserName']. " Address=".$_SERVER['REMOTE_ADDR']. $s);
		closelog();
	}
}

?>
